package com.medical.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.medical.dao.SysUserMapper;
import com.medical.entity.SysUser;
import com.medical.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * 获取用户信息列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/getUserList")
	public String getUserList(Model model) {
		model.addAttribute("userList", userService.getUserList());
		return "/user/user_list";
	}
	
	/**
	 * 打开添加页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/openAdd")
	public String openAdd(Model model) {
		return "/user/user_add";
	}
	
	/**
	 * 添加用户信息
	 * @param sysUser
	 * @param model
	 * @return
	 */
	@RequestMapping("/add")
	public String add(SysUser sysUser, Model model) {
		userService.addUser(sysUser);
		return getUserList(model);
	}
	
	/**
	 * 打开编辑页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/openEdit")
	public String openEdit(Integer id, Model model) {
		SysUser sysUser = userService.getUserById(id);
		model.addAttribute("user",sysUser);
		return "/user/user_edit";
	}
	
	/**
	 * 更新
	 * @param sysUser
	 * @param model
	 * @return
	 */
	@RequestMapping("/edit")
	public String edit(SysUser sysUser, Model model) {
		userService.edit(sysUser);
		return getUserList(model);
	}
	
	/**
	 * 删除
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(Integer id, Model model) {
		userService.delete(id);
		return getUserList(model);
	}

}
